#include "form_recover.h"
#include "printer.h"
#include "qmenu.h"
#include "ui_form_recover.h"
#include <QFileDialog>
Form_recover::Form_recover(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form_recover)
{
    ui->setupUi(this);
    setWindowModality(Qt::ApplicationModal);
    ui->tableWidget_selects->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget_selects->verticalHeader()->setVisible(false);
    QIntValidator* validator =new QIntValidator(1,100,parent);
    ui->lineEdit->setValidator(validator);
    setWindowIcon(QIcon(":/icon/recoverSelect.png"));
    fun=[this](std::string s){textEdit->append(QString::fromStdString(s));};
}

Form_recover::~Form_recover()
{
    delete ui;
}

void Form_recover::closeEvent(QCloseEvent *event)
{
    if(event)
        emit closeSignal();
}

void Form_recover::on_pushButton_setNum_clicked()
{
    auto s=ui->lineEdit->text();
    if(s.isEmpty()){
        return;
    }
    Printer* logger=Printer::init();
    logger->print(this->textEdit,"授权集合编号："+s,Printer::L_INFO);
    setNum=s.toInt();
    ui->label_curSetNumRight->setText(std::move(s));
}


void Form_recover::on_tableWidget_selects_cellClicked(int row, int column)
{
    if(row<cnt&&column==0){
        QPoint globalPos = QCursor::pos();
        // 创建自定义上下文菜单
        QMenu contextMenu;
        // 添加菜单项
        QAction* action1 = contextMenu.addAction("删除");
        action1->setIcon(QIcon(":/icon/delete.png"));
        // 显示菜单，并获取用户选择的菜单项
        QAction* selectedItem = contextMenu.exec(globalPos);
        // 根据用户选择的菜单项执行相应操作
        if (selectedItem == action1) {
            // 执行菜单项1的操作
            --cnt;
            ui->tableWidget_selects->removeRow(row);
        }
    }
}

void Form_recover::on_pushButton_recover_recover_clicked()
{
    emit closeSignal();
}

void Form_recover::on_tableWidget_selects_customContextMenuRequested(const QPoint &pos)
{
    // 创建自定义上下文菜单
    QMenu contextMenu;
    // 添加菜单项
    QAction* action1 = contextMenu.addAction("添加子秘密");
    action1->setIcon(QIcon(":/icon/add.png"));
    QAction* action2 = contextMenu.addAction("清空子秘密");
    action2->setIcon(QIcon(":/icon/clear.png"));
    // 显示菜单，并获取用户选择的菜单项
    QAction* selectedItem = contextMenu.exec(ui->tableWidget_selects->mapToGlobal(pos));
    // 根据用户选择的菜单项执行相应操作
    if (selectedItem == action1) {
        // 执行菜单项1的操作
        auto imagePos= QFileDialog::getOpenFileNames(nullptr, QString("选择文件"),QDir::currentPath(), "图片文件 (*.png *.jpg *.jpeg *.bmp *.pgm *.pbm *.txt)",nullptr,QFileDialog::ShowDirsOnly);
        if(!imagePos.empty()){
            int n=imagePos.size();
            for(int i=0;i<n;++i){
                ui->tableWidget_selects->insertRow(cnt);
                ui->tableWidget_selects->setItem(cnt,0,new QTableWidgetItem(imagePos[i]));
                ++cnt;
            }
        }
    }else if(action2==selectedItem){
        cnt=0;
        ui->tableWidget_selects->setRowCount(0);
    }
}
